/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 4/16/13
 * Time: 8:19 AM
 * To change this template use File | Settings | File Templates.
 */
package societyGames.xmlFragment
{
import mx.collections.IList;

public class XMLFragmentEditList extends XMLFragmentCollection implements IEditList
{
  //IEditList-->
  public function get editItems():IList
  {
    return this;
  }

  public function addEditItem():Object
  {
    return addEditItemFromString(null);
  }

  public function removeEditItem(item:Object):void
  {
    removeItemAt(getItemIndex(item));
  }

  public function moveEditItemUp(item:Object):Boolean
  {
    //Get item index.
    var index:int = this.getItemIndex(item);
    //See if valid range
    if (index > 0)
    {
      removeItemAt(index) ;
      addItemAt(item, index - 1);
      return true;
    }
    return false;
  }

  public function moveEditItemDown(item:Object):Boolean
  {
    //Get item index.
    var index:int = getItemIndex(item);
    //See if valid range
    if (index >= 0 && index < list.length - 1)
    {
      //Swap by moving next element up by 1.
      removeItemAt(index);
      addItemAt(item, index + 1);
      return true;
    }
    return false;
  }

  /**
   * Gets a string representation of the edit item.
   * @return String representation of the edit item.
   */
  public function getEditItemAsString(item:Object):String
  {
    var xmlFragment:XMLFragment = item as XMLFragment;
    if (xmlFragment == null)
    {
      throw new ArgumentError("Invalid item");
    }
    return xmlFragment.xml.toXMLString();
  }

  /**
   * Adds an edit item from string.
   * @param text String representation of edit item, from getEditItemAsString
   */
  public function addEditItemFromString(text:String):Object
  {
    var editItem:XMLFragment = _xmlContainer.createChildXMLFragment(_childClass, text != null ? XML(text) : null);
    addItem(editItem);
    return editItem;
  }

  //<--IEditList
}
}
